package com.dy.design_pattern_study.prototype_mode.dome2;

import com.dy.design_pattern_study.utils.Utils;
import lombok.Data;

/**
 * 浅克隆类
 * @CreateBy zhangdingyi
 * @Date 2023-03-23
 * @Content 简历类，简历可以 clone 且不用新建一个对象
 * 一般在初始化的信息不发生变化的情况下，克隆是最好的办法。
 * 这既隐藏了对象创建的细节，又对性能是大大的提高
 * 不用重新初始化对象，而是动态地获得对象运行时的状态
 * -浅克隆
 * 这里的话实质就是在克隆的时候，仅克隆非引用对象，这种就是浅克隆（因为引用对象克隆的时候仅克隆了引用，而没有克隆本体）
 */
@Data
public class Resume implements Cloneable {

    public static void main(String[] args) throws CloneNotSupportedException {
        Resume resume = new Resume("张三");
        resume.setPersonalInfo("男", "23");
        resume.setWorkExperience("2023-03-23", "企业外包");

        resume.setCity(new City());
        resume.setCityData("柳州", "广西");

        Resume resume1 = (Resume) resume.Clone();
        resume1.setWorkExperience("2022-04-20", "XX企业");
        resume1.setCityData("柳州2", "广西2");

        Resume resume2 = (Resume) resume1.Clone();
        resume2.setPersonalInfo("女", "50");

        //以最后一个生效 -说明City是一个引用类型，引用类型的地址不会跟着进行clone
        resume2.setCityData("柳州3", "广西3");

        resume.display();
        resume1.display();
        resume2.display();

        System.out.println("resume Address = " + Utils.<Resume>getAddress(resume));
        System.out.println("resume1 Address = " + Utils.<Resume>getAddress(resume1));
        System.out.println("resume2 Address = " + Utils.<Resume>getAddress(resume2));

    }

    private String name;
    private String sex;
    private String age;
    private String timeArea;
    private String company;

    private City city;

    public Resume(String name) {
        this.name = name;
    }

    public void setPersonalInfo(String sex, String age) {
        this.sex = sex;
        this.age = age;
    }

    public void setWorkExperience(String timeArea, String company) {
        this.timeArea = timeArea;
        this.company = company;
    }

    public void setCityData(String name, String adress) {
        city.setCityName(name);
        city.setCityAddress(adress);
    }

    public void display() {
        System.out.println("----------简历开始----------------");
        System.out.println("name = " + name);
        System.out.println("sex = " + sex);
        System.out.println("age = " + age);
        System.out.println("timeArea = " + timeArea);
        System.out.println("company = " + company);
        System.out.println("CityName = " + city.getCityName());
        System.out.println("CityAddress = " + city.getCityAddress());
        System.out.println("----------简历结束----------------");
    }

    public Object Clone() throws CloneNotSupportedException {
        return (Object) this.clone();
    }
}
